#! /usr/bin/python
# -*- coding: utf-8 -*-
"""
    Author: wanzhang
    Email: wanzhang@newtas.net
    Data: 2012-11-09
    Usage: wc.py filename
"""
import sys
import string
from optparse import OptionParser

#set command line
usage = "usage: wc [option]... [file]..."
opt_parser = OptionParser(usage=usage)
opt_parser.add_option('-c', '--char', help="print the character counts")
opt_parser.add_option('-l', '--line', help="print the line counts")
opt_parser.add_option('-w', '--word',help="print the word counts")

#count words/chars/lines
def get_wcl_count():
    [options, args] = opt_parser.parse_args(sys.argv)
    if len(sys.argv) == 1:
        print("Try use --help !")
        sys.exit()
    else:
        if(options.char or options.word or options.line):
            file_start = 2
        else:
            file_start = 1
        for file_name in sys.argv[file_start:]:
            lines_each_count = 0
            words_each_count = 0
            chars_each_count = 0
            for each_line in open(file_name):
                lines_each_count +=1
                for each_char in each_line:
                    chars_each_count +=1
                for each_word in each_line.split():
                    words_each_count +=1
            if options.char:
                print "%d " % chars_each_count,
            elif options.word:
                print "%d " % words_each_count,
            elif options.line:
                print "%d " % lines_each_count,
            else:
                print "%d %d %d " %(lines_each_count, words_each_count, chars_each_count),
            print "%s" % file_name
                 
          
def main():
    
    get_wcl_count()

if __name__ == '__main__':
    main()    
